package com.exposit.books.web;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.exposit.books.service.OrderService;

@Controller
public class ReturnController {
	
	@Autowired
	private OrderService orderService;

	@RequestMapping(value = "/return/{orderID}", method= RequestMethod.GET)
	public ModelAndView cancel(
			@PathVariable("orderID") String orderId,
			HttpSession session, Model model) {
		if (session.getAttribute("user") == null)
		{
			ModelAndView mav = new ModelAndView();
			mav.setViewName("errorPage");
			return mav;
		}
		ModelAndView mav = new ModelAndView("redirect:/return/"+orderId);
		mav.addObject("order", orderService.getOrderByID(Long.parseLong(orderId)));
		mav.setViewName("return");
		return mav;
	}
}
